﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KZ.Express.H.Presenter;
using KZ.Utilities;
using KZ.Express.H.ENT;

namespace KZ.Express.H.Orders
{
    public partial class UpdateFuelView : BaseView, IBaseView
    {
        private void UpdateFuelView_Load(object sender, EventArgs e)
        {
            dtpFrom.Value = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
            dtpTo.Value = new DateTime(DateTime.Today.Year, DateTime.Today.AddMonths(1).Month, 1).AddDays(-1);
            LoadCarrier();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            string msg = String.Empty;
            if (Utility.IntParse(cmbCarrierID.SelectedValue) == 0)
                msg = "Bạn phải chọn dịch vụ";
            if (Utility.DecimalParse(txtFSC.Text) == 0)
                msg += "\nBạn phải nhập FSC";

            if (!String.IsNullOrEmpty(msg))
                this.ShowMessageBoxError(msg);

            SearchCriteria search = new SearchCriteria();
            search.Add("From", dtpFrom.Value);
            search.Add("To", dtpTo.Value);
            search.Add("CarrierID", Utility.IntParse(cmbCarrierID.SelectedValue));
            search.Add("FuelPercent", Utility.DecimalParse(txtFSC.Text));
            search.Add("UserID", UserENT._USERID);

            _presenter.ExecuteNonQuery(OrderENT.UpdateFuelProcedure, search);
            DialogResult = DialogResult.OK;
        }

        private void LoadCarrier()
        {
            try
            {
                cmbCarrierID.ValueMember = "CarrierID";
                cmbCarrierID.DisplayMember = "CarrierName";
                DataTable dt = _presenter.LoadDataTable<CarrierENT>(null);
                DataRow row = dt.NewRow();
                dt.Rows.InsertAt(row, 0);
                cmbCarrierID.DataSource = dt;
            }
            catch (BusinessLogicException ex)
            {
                ShowMessageBoxError(ex.Message);
            }
        }

        public UpdateFuelView()
        {
            InitializeComponent();
            _presenter = new BasePresenter(this);
        }
    }
}
